package com.orange.off.cloud.cloudoff.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.orange.off.cloud.cloudoff.R;
import com.orange.off.cloud.cloudoff.viewpagerindicator.PageIndicator;

import java.util.ArrayList;
import java.util.List;


/**
 * 首次打开程序操作提示
 *
 * @author smile
 */
public class HelpActivity extends Activity implements OnClickListener {
    private ViewPager mPager;
    private PageIndicator mIndicator;
    private List<View> views = new ArrayList<View>();
    public final static String ISFIRST_KEY = "isfirst";
    private SharedPreferences mPreferences;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        // 取消状态栏
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.help);
        mPager = (ViewPager) findViewById(R.id.help_viewpager);
        mIndicator = (PageIndicator) findViewById(R.id.help_indicator);
        mPreferences = getSharedPreferences(getString(R.string.version_name), 0);
        // 标记是否第一次进入该页面 TODO: 正式发布时再改 为true
        mPreferences.edit().putBoolean(ISFIRST_KEY, true).commit();
        mPager.setAdapter(new HelpAdapter(this));
        LayoutInflater mLi = LayoutInflater.from(this);
        View start = mLi.inflate(R.layout.help_view, null);
        View btnstart = start.findViewById(R.id.startBtn);
        btnstart.setOnClickListener(this);
        mPager.addView(start);
        mIndicator.setViewPager(mPager);

    }

    class HelpAdapter extends PagerAdapter {

        public HelpAdapter(Context context) {
            LinearLayout.LayoutParams mParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                    LinearLayout.LayoutParams.MATCH_PARENT);
            int[] images = {R.drawable.yindao01, R.drawable.yindao02};
            for (int i = 0; i < images.length; i++) {
                ImageView view = new ImageView(context);
                view.setLayoutParams(mParams);
                view.setImageResource(images[i]);
                view.setScaleType(ImageView.ScaleType.FIT_XY);
                views.add(view);
            }
            LayoutInflater mLi = LayoutInflater.from(context);
            View start = mLi.inflate(R.layout.help_view, null);
            View btnstart = start.findViewById(R.id.startBtn);
            btnstart.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(getApplicationContext(), IntermediaryActivity.class);
                    intent.putExtra("type", IntermediaryActivity.LOGIN);
                    startActivity(intent);
                    //                    Intent intent = new Intent(HelpActivity.this, MainActivity.class);
                    //                    startActivity(intent);
                    finish();
                    overridePendingTransition(R.anim.anim_enter, R.anim.anim_exit);
                }
            });
            views.add(start);
        }

        @Override
        public int getCount() {
            return views.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view.equals(object);
        }

        @Override
        public Object instantiateItem(View container, int position) {

            ((ViewPager) container).addView(views.get(position), 0);
            return views.get(position);
        }

        @Override
        public void destroyItem(View container, int position, Object object) {

            ((ViewPager) container).removeView(views.get(position));
        }

    }

    @Override
    public void onClick(View v) {
        // Intent intent = new Intent(HelpActivity.this,HomeActivity.class);
        // startActivity(intent);
        // finish();
    }

    @Override
    protected void onPause() {
        super.onPause();
        // MobclickAgent.onPause(this);

    }

    @Override
    protected void onResume() {
        super.onResume();
        // MobclickAgent.onResume(this);
    }

}
